home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6888 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1008 b   |  50 lines

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: overloading new operator
  4. Message-ID: <1996Feb20.112926.1780@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 20 Feb 96 11:29:26 WET
  7. References: <marnoldDn0A8r.Ipu@netcom.com>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <marnoldDn0A8r.Ipu@netcom.com> marnold@netcom.com (Matt Arnold)  
  12. writes:
  13.  
  14. [snip: explanation of `placement new']
  15.  
  16. > This form allows you to write code like this...
  17. >    char my_memory[100];
  18. >    new(my_memory) MyObject;
  19.  
  20. Please note that this is not guaranteed to work. Apart from making sure we  
  21. provide enough memory for an instance of the MyObject class, we must  
  22. guarantee the memory is suitably aligned:
  23.  
  24. union Align {     // will work on (probably) all architectures
  25.     char c;
  26.     short s;
  27.     int i;
  28.     long l;
  29.     float f;
  30.     double d;
  31.     long double ld;
  32.     void *p;
  33.     void (*pf)();
  34. };
  35.  
  36. void f()
  37. {
  38.     union {
  39.         Align a;
  40.         char buf[sizeof(MyObject)];
  41.     } my_memory;
  42.  
  43.     new (&my_memory) MyObject;
  44. }
  45.  
  46. - Wil
  47.